package com.black.knight.chess.providers;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.black.knight.chess.domain.PNGGame.PNGGames;

public class PNGGamesContentProvider extends ContentProvider {

	String TAG = "PNGGamesProvider";
	
    public static final String AUTHORITY = "com.black.knight.chess.providers.PNGGamesContentProvider";
    public static final String PNG_GAMES_TABLE_NAME = "pnggames";
    
    private static final UriMatcher sUriMatcher;
    private static final int PNG_GAMES = 3;
    private static HashMap<String, String> pngGamesProjectionMap;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, PNG_GAMES_TABLE_NAME, PNG_GAMES);

        pngGamesProjectionMap = new HashMap<String, String>();
        pngGamesProjectionMap.put(PNGGames.PNG_GAME_ID, PNGGames.PNG_GAME_ID);
        pngGamesProjectionMap.put(PNGGames.DESCRIPTION, PNGGames.DESCRIPTION);
        pngGamesProjectionMap.put(PNGGames.CATEGORY, PNGGames.CATEGORY);
        pngGamesProjectionMap.put(PNGGames.PNG, PNGGames.PNG);
        pngGamesProjectionMap.put(PNGGames.FEN, PNGGames.FEN);
    }
    
    private DatabaseHelper dbHelper;
    
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case PNG_GAMES:
                count = db.delete(PNG_GAMES_TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
	        case PNG_GAMES:
	            return PNGGames.CONTENT_TYPE;
	        default:
	            throw new IllegalArgumentException("Unknown URI " + uri);
		}

	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		 if (sUriMatcher.match(uri) != PNG_GAMES) { 
			 throw new IllegalArgumentException("Unknown URI " + uri); 
		 }

	    ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long rowId = db.insert(PNG_GAMES_TABLE_NAME, PNGGames.DESCRIPTION, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(PNGGames.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		dbHelper = new DatabaseHelper(getContext());
        return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (sUriMatcher.match(uri)) {
            case PNG_GAMES:
                qb.setTables(PNG_GAMES_TABLE_NAME);
                qb.setProjectionMap(pngGamesProjectionMap);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

    	SQLiteDatabase db = dbHelper.getWritableDatabase();
    	
    	try {
    		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

    		c.setNotificationUri(getContext().getContentResolver(), uri);
        	
        	return c;
    	} catch (Exception e) {
			e.toString();
		}
    	
    	return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case PNG_GAMES:
                count = db.update(PNG_GAMES_TABLE_NAME, values, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;

	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] initialValues) {
		 if (sUriMatcher.match(uri) != PNG_GAMES) { 
			 throw new IllegalArgumentException("Unknown URI " + uri); 
		 }

	    ContentValues[] values;
        if (initialValues != null) {
            values = initialValues;
        } else {
            values = new ContentValues[0];
        }
        
        int inserted = 0;
        
        for (ContentValues contentValues : values) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
           
            try {
            	long rowId = db.insert(PNG_GAMES_TABLE_NAME, PNGGames.DESCRIPTION, contentValues);
	            if (rowId > 0) {
	                inserted++;
	            }
            } catch (SQLException e) {
			}
		}
        
        return inserted;
	}
}
